package com.csdj.server.service.impl;
import java.time.LocalDateTime;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csdj.server.mapper.EmployeeMapper;
import com.csdj.server.mapper.MailLogMapper;
import com.csdj.server.pojo.*;
import com.csdj.server.service.IEmployeeService;
import com.csdj.server.service.IMailLogService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyq
 * @since 2022-12-12
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired(required = false)
    private MailLogMapper mailLogMapper;

    @Override
    public RespPageBean getEmployeeByPage(Integer currentPage,
                                          Integer size,
                                          Employee employee,
                                          LocalDate[] beginDateScope) {
        Page<Employee> page=new Page<>(currentPage,size);

        IPage<Employee> employeeByPage = employeeMapper
                        .getEmployeeByPage(page, employee, beginDateScope);

        RespPageBean respPageBean=new RespPageBean(employeeByPage.getTotal(),
                employeeByPage.getRecords());

        return respPageBean;
    }

    @Override
    public RespBean maxWorkId() {

         //list 集合 存储的是 一个 map集合
        //map集合中存放的数据 key -value
        //key =max(workID) value=000000100
        List<Map<String, Object>> maps = employeeMapper.selectMaps(
                new QueryWrapper<Employee>().select("max(workID)"));

      //Integer maxWorkId=  Integer.parseInt( maps.get(0).get("max(workID)").toString())+1;
        //101
        // %08d 解决 转换 String类型的时候 出现乱码
        //补0
        return RespBean.success(null,String.format("%08d",
                Integer.parseInt( maps.get(0)
                        .get("max(workID)").toString())+1));
    }

    /**
     * 添加员工信息
     * @param employee
     * @return
     */
    @Override
    public RespBean insertEmp(Employee employee) {
        //获取合同期限的起始日期
        LocalDate beginContract = employee.getBeginContract();
        //合同的期限的结束日期
        LocalDate endContract = employee.getEndContract();
        //计算 两个日期直接 相差的天数
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        //指定计算 年限之后 保留两位小数
        DecimalFormat decimalFormat=new DecimalFormat("##.00");
        //设置 合同年限
        employee.setContractTerm(Double.parseDouble(
                decimalFormat.format(days/365.00)));
        //新增员工信息
        if(employeeMapper.insert(employee)==1){
            //消息发送
            Employee emp = employeeMapper.getAllEmployee(employee.getId()).get(0);
            MailLog mailLog = new MailLog();
            //
            String msgId = UUID.randomUUID().toString();
            mailLog.setMsgId(msgId);
            mailLog.setEid(emp.getId());
            mailLog.setStatus(0);
            mailLog.setRouteKey(MailConstants.MAIL_ROUTINGKEY_NAME);
            mailLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailLog.setCount(0);
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstants.MAX_TIMEOUT));
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());
            mailLogMapper.insert(mailLog); //消息落库
            //通过 交换机 指定路由 推送信息
            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME,
                    MailConstants.MAIL_ROUTINGKEY_NAME, emp,
                    new CorrelationData(msgId));
            return RespBean.success("添加成功!");
        }
        return RespBean.error("添加失败!");
    }

    /**
     * 根据 员工编号 查询 员工信息
     * @param id
     * @return
     */
    @Override
    public List<Employee> getAllEmployee(Integer id) {

        return employeeMapper.getAllEmployee(id);
    }

    @Override
    public RespPageBean getEmployeeWithSalary(Integer current, Integer size) {

        Page<Employee> page = new Page<>(current, size);
        IPage<Employee> allEmployeeWithSalary =
                employeeMapper.getAllEmployeeWithSalary(page);
        RespPageBean respPageBean = new RespPageBean(
                allEmployeeWithSalary.getTotal(),
                allEmployeeWithSalary.getRecords());

        return respPageBean;
    }
}
